package module.building.view.Recruit
{
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.UIManager;
	import com.darwin.darwinUI.uiconst.UIAlign;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import module.building.event.BuildEvent;
	import module.building.view.produceweapon.ProduceWeaponNeed;
	import module.building.view.produceweapon.WeaponProducePanel;
	import module.main.view.ResourceConst;

	public class RecruiteArmsPanel extends UIComponent
	{
		/**
		 *背景面板 
		 */		
		private var _recruiteArmsBg:UIComponent;
		/**
		 *招募士兵背景 
		 */		
		private var _recruiteTitelBg:UIComponent;
		/**
		 *招募士兵标题 
		 */		
		private var _recruiteTitelLabel:Label;
		/**
		 *放置一个个部件的背景 
		 */		
		private var _setItemBg:UIComponent;
		/**
		 *关闭按钮 
		 */		
		private var _closeBtn:Button;
		public function RecruiteArmsPanel()
		{
			_recruiteArmsBg = new UIComponent();
			_recruiteArmsBg.backgroundSkin = ResourceConst.BUILD_IMAGE_FRAME_BG;
			_recruiteArmsBg.height = 441.8;
			_recruiteArmsBg.width = 461.6;
			
			_recruiteTitelBg = new UIComponent()
			_recruiteTitelBg.backgroundSkin = ResourceConst.BUILD_IMAGE_FRAME_TITLE;
			_recruiteTitelBg.width = 189.9;
			_recruiteTitelBg.height = 55.0;
			
			_recruiteTitelLabel = TextFieldFactory.getInstance().getLabel("militia_title");
			
			_setItemBg = new UIComponent();
			_setItemBg.backgroundSkin = ResourceConst.BUILD_IMAGE_FRAME_BG2;
			_setItemBg.height = 382.3;
			_setItemBg.width = 425.0;
			
			_closeBtn = new Button();
			_closeBtn.backgroundSkin = ResourceConst.BUILD_BUTTON_CLOSE;
			_closeBtn.backgroundAuto = true;
			_closeBtn.height = 23.9;
			_closeBtn.width = 25.9;
			
			recruitArr = new Array();
			
			this.height = 458.8;
			this.width = 461.6;
			setPosition();
			initListen();
		}
		private function initListen():void
		{
			this.addEventListener(Event.REMOVED_FROM_STAGE,removeHandler);
		}
		private function removeHandler(e:Event):void
		{
			removeAllChild();
		}
		/**
		 *设置组件位置 
		 * 
		 */		
		private function setPosition():void
		{
			setRecruiteArmsBg();
			setRecruiteTitelBg();
			setPutItemBg();
			setCloseBtn();
			setRecruiteItem();
		}
		private function setRecruiteArmsBg():void
		{
			_recruiteArmsBg.y = 18;
			addChild(_recruiteArmsBg);
		}
		private function setRecruiteTitelBg():void
		{
			_recruiteTitelBg.x = (this.width - _recruiteTitelBg.width) / 2;
			_recruiteTitelLabel.text = "招募士兵";
			_recruiteTitelLabel.x = (_recruiteTitelBg.width - _recruiteTitelLabel.width) / 2;
			_recruiteTitelLabel.y = (_recruiteTitelBg.height - _recruiteTitelLabel.height) / 2;
			addChild(_recruiteTitelBg);
			_recruiteTitelBg.addChild(_recruiteTitelLabel);
		}
		private function setPutItemBg():void
		{
			_setItemBg.x = (this.width - _setItemBg.width) / 2;
			_setItemBg.y = _recruiteTitelBg.height + 2;
			addChild(_setItemBg);
		}
		private function setCloseBtn():void
		{
			_closeBtn.x = this.width - 40;
			_closeBtn.y = 30;
			addChild(_closeBtn);
			_closeBtn.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		/**
		 *存储收获ITEM的实例 
		 */		
		private var recruitArr:Array;
		private function setRecruiteItem():void
		{
			for(var i:uint = 0;i<6;i++)
			{
				var $panlist:RecruitArmsItem = new RecruitArmsItem();
				if(i<=2)
				{
					$panlist.x = 30 + $panlist.width * i - 2*i;
					$panlist.y = 65;
				}
				else
				{
					$panlist.x = 30 + $panlist.width * (i - 3) - 2*(i - 3);
					$panlist.y = 246;
				}
				recruitArr.push($panlist);
				addChild($panlist);
				$panlist.addEventListener(BuildEvent.PRODUCE,produceHandler);
			}
		}
		private function produceHandler(e:BuildEvent):void
		{
			var evt:BuildEvent = new BuildEvent(BuildEvent.PRODUCE);
			evt.data = e.data;
			this.dispatchEvent(evt);
		}
		private function clickHandler(e:MouseEvent):void
		{
			UIManager.getPopUpManager().removePopUp(this);
		}
		/**
		 *对controller提供一个修改数据的接口 
		 * @param obj
		 * 
		 */
		private var _getArr:Array;
		public function changRecruitData(arr:Array):void
		{
			_getArr = arr;
			for(var i:int = 0;i<recruitArr.length;i++)
			{
				var $recruit:RecruitArmsItem = recruitArr[i] as RecruitArmsItem;
				$recruit.changeData(_getArr[i + 8]);
			}
		}
	}
}